package net.lab1024.sa.admin.module.workflow.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.lab1024.sa.admin.module.workflow.dao.WfFormDao;
import net.lab1024.sa.admin.module.workflow.domain.entity.WfForm;
import net.lab1024.sa.admin.module.workflow.domain.form.WfFormBo;
import net.lab1024.sa.admin.module.workflow.domain.form.WfFormVo;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 流程表单Service业务层处理
 *
 * @author KonBAI
 * @createTime 2022/3/7 22:07
 */
@RequiredArgsConstructor
@Service
public class WfFormService {

    @Resource
    private final WfFormDao wfFormDao;

    /**
     * 查询流程表单
     *
     * @param formId 流程表单ID
     * @return 流程表单
     */

    public WfForm queryById(Long formId) {
        return wfFormDao.selectById(formId);
    }

    public WfForm queryByProcKey(String procKey) {
        return wfFormDao.selectOne(Wrappers.<WfForm>lambdaQuery().eq(WfForm::getProcessKey, procKey));
    }

    /**
     * 查询流程表单列表
     *
     * @param bo 流程表单
     * @return 流程表单
     */

    public PageResult<WfFormVo> queryPageList(WfFormBo bo) {
        Page<?> page = SmartPageUtil.convert2PageQuery(bo);
        LambdaQueryWrapper<WfForm> lqw = buildQueryWrapper(bo);
        List<WfFormVo> result = wfFormDao.selectFormVoList(lqw);
        PageResult<WfFormVo> pageResult = SmartPageUtil.convert2PageResult(page, result);

        return pageResult;
    }

    /**
     * 查询流程表单列表
     *
     * @param bo 流程表单
     * @return 流程表单
     */

    public List<WfForm> queryList(WfFormBo bo) {
        LambdaQueryWrapper<WfForm> lqw = buildQueryWrapper(bo);
        return wfFormDao.selectList(lqw);
    }

    /**
     * 新增流程表单
     *
     * @param bo 流程表单
     * @return 结果
     */
    public ResponseDTO<Object> insertForm(WfFormBo bo) {

        WfForm wfForm1 = wfFormDao.selectOne(Wrappers.<WfForm>lambdaQuery().eq(WfForm::getProcessKey, bo.getProcessKey()));
        if (wfForm1 != null) {
            return ResponseDTO.userErrorParam("该流程以配置表单");
        }
        WfForm copy = SmartBeanUtil.copy(bo, WfForm.class);
        return ResponseDTO.ok(wfFormDao.insert(copy));
    }

    /**
     * 修改流程表单
     *
     * @param bo 流程表单
     * @return 结果
     */

    public int updateForm(WfFormBo bo) {

        WfForm copy = SmartBeanUtil.copy(bo, WfForm.class);
        return wfFormDao.updateById(copy);


    }

    /**
     * 批量删除流程表单
     *
     * @param ids 需要删除的流程表单ID
     * @return 结果
     */

    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return wfFormDao.deleteBatchIds(ids) > 0;
    }

    private LambdaQueryWrapper<WfForm> buildQueryWrapper(WfFormBo bo) {
        LambdaQueryWrapper<WfForm> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFormName()), WfForm::getFormName, bo.getFormName());
        return lqw;
    }
}
